import GameManager, { GameState, GameType } from "./GameManager";
import TipPanel from "../UI/TipPanel";
import AudioManager from "./AudioManager";
import WinLosePanel from "../UI/WinLosePanel";
import Cell from "../Core/Cell";
import CellFunctions from "../Core/CellFunctions";
import LoadingPanel from "../UI/LoadingPanel";
import StartPanel from "../UI/StartPanel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class InputManager extends cc.Component {
    public static Instance: InputManager;

    onLoad() {
        InputManager.Instance = this;
    }

    start() {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    }

    public compare(a, b) {
        if (a < b) {           // 按某种排序标准进行比较, a 小于 b
            return -1;
        }
        if (a > b) {
            return 1;
        }
        return 0;
    }

    public onKeyDown(event: KeyboardEvent, customDate): void {
        // cc.log(event);
        if (event.keyCode == cc.macro.KEY.w || event.keyCode == cc.macro.KEY.up) {
            // TipPanel.Instance.Show("上");
            // CellFunctions.CreateTopLine();
        }

        if (event.keyCode == cc.macro.KEY.s || event.keyCode == cc.macro.KEY.down) {
            // TipPanel.Instance.Show("下");
            CellFunctions.ControlMoveDown();
        }

        if (event.keyCode == cc.macro.KEY.a || event.keyCode == cc.macro.KEY.left) {
            // TipPanel.Instance.Show("左");
            CellFunctions.MoveLefOrRight(-1);
        }

        if (event.keyCode == cc.macro.KEY.d || event.keyCode == cc.macro.KEY.right) {
            // TipPanel.Instance.Show("右");
            CellFunctions.MoveLefOrRight(+1);
        }

        if (event.keyCode == cc.macro.KEY.space) {
            // LoadingPanel.Instance.ShowOff();
            // StartPanel.Instance.ShowOn();
            // AudioManager.Instance.LoadVolume();
            // AudioManager.Instance.SetVolume();

            // AudioManager.Instance.audio_fall.loop = true;
            AudioManager.Instance.audio_fall.play();
            AudioManager.Instance.audio_fall.loop = true;
        }

        if (event.keyCode == cc.macro.KEY.b) {
            let a = "111"
            let b = Number(a) + 2;
            console.log(b);
        }

        if (event.keyCode == cc.macro.KEY.n) {

        }

        if (event.keyCode == cc.macro.KEY.m) {

        }

        if (event.keyCode == cc.macro.KEY.num1) {
            cc.log(GameState[GameManager.Instance.gameState]);
        }

        if (event.keyCode == cc.macro.KEY.num2) {
            GameManager.Instance.keyCode = cc.macro.KEY.num2;
        }

    }

    public onKeyUp(event: KeyboardEvent, customDate): void {
       
    }
}
